package com.victor.pomelo.dao;

import java.util.List;
import java.util.Set;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.victor.pomelo.model.Log;
import com.victor.pomelo.model.Resource;
import com.victor.pomelo.model.User;

public interface UserMapper {
	@Insert("insert into tb_user (userId, organId, username, password, salt, nickname, isonline, times, logtime, loginip, state, cretime, updtime, updreason)"
			+ " values "
			+ "(#{userId}, #{organId}, #{username}, #{password}, #{salt}, #{nickname}, #{isonline}, #{times}, #{logtime}, #{loginip}, #{state}, #{cretime}, #{updtime}, #{updreason})")
	public int insert(User user);

	@Update("update tb_user set userId = #{userId}, organId = #{organId}, username = #{username}, password = #{password}, salt = #{salt}, nickname = #{nickname}, isonline = #{isonline}, times = #{times}, logtime = #{logtime}, loginip = #{loginip}, state = #{state}, cretime = #{cretime}, updtime = #{updtime}, updreason = #{updreason "
			+ "where userId = #{userId}")
	public int update(User user);

	@Delete("delete from tb_user where userId = #{userId}")
	public int delete(Long id);

	@Select("select from tb_user where userId = #{userId}")
	public User findById(@Param("userId") Long userId);

	// public int updateByIdWithBLOBs(User user);

	@Select("select * from tb_user")
	public List<User> findAll();

	@Select("select * from tb_user where username = #{username}")
	public User findByUsername(String username);

	/**
	 * 通过用户名称查询用户权限
	 * 
	 * @param username
	 * @return
	 */
	@Select("select permission from tb_role_permission rp where rp.state = '1' and exists (select * from tb_user_role ur where ur.roleid = rp.roleid and ur.userid = #{userId})"
			+ " union"
			+ " select permission from tb_group_permission gp where gp.state = '1' and exists (select * from tb_user_group ug where ug.groupid = gp.groupid and ug.userid = #{userId})"
			+ " union"
			+ " select permission from tb_user_permission up where up.state = '1' and up.userid = #{userId}")
	public Set<String> findPermissionById(Long userId);

	/**
	 * 分页查询用户组下用户信息列表
	 * 
	 * @param groupid
	 * @param start
	 * @param offset
	 * @return
	 */
	@Select("select * from tb_user t where exists (select * from tb_user_group x where groupid=#{groupid} t.userid = x.userid) limit  #{start}, #{offset}")
	public List<User> findByGroupid(@Param(value = "groupid") Long groupid,
			@Param(value = "start") Long start,
			@Param(value = "offset") Long offset);

	/**
	 * 查询用户组下的用户数
	 * 
	 * @param groupid
	 * @return
	 */
	@Select("select count(*) from tb_user t where exists (select * from tb_user_group x where groupid = #{groupid} t.userid = x.userid)")
	public Long findCountByGroupid(@Param(value = "groupid") Long groupid);

	/*
	 * @Results(value = {
	 * 
	 * @Result(id = true, property = "userId", column = "user_Id"),
	 * 
	 * @Result(id = true, property = "organId", column = "organ_Id") })
	 * 原来因为字段命名的时候使用了下横线所以必须用显式定义转换字段对应，后将数据变更顾不需要定义
	 */

	/*	*//**
	 * 通过用户ID查询用户可以使用的菜单资源信息
	 * 
	 * @param userId
	 * 
	 * @return
	 */
	/*
	 * @Select(
	 * "select * from TB_RESOURCE res where exists(select * from tb_role_resource a,tb_user_role b where a.roleId=b.roleId and a.resourceId=res.resourceId and b.userId=#{userId})"
	 * ) public List<Resource> findMenusByUserId(@Param(value = "userId") Long
	 * userId);
	 */
	/*
	 * @Select("select * from TB_LOG") public List<Log> findLog();
	 */
	@Select("select * from tb_user limit #{start}, #{offset}")
	public List<User> findAll1(@Param(value = "start") Long start,
			@Param(value = "offset") Long offset);

}